
$(function () {
    let clickOne = false;
    editormd.markdownToHTML("content", {emoji: true});
    let url = window.location.href;
    let id = url.substring(url.lastIndexOf("/") + 1, url.length);
    $.ajax({
        //请求方式
        type: "get",
        //请求的媒体类型
        contentType: "application/json;charset=UTF-8",
        //请求地址
        url: "/blog/likes/" + id,
        //数据，json字符串
        dataType: "json",
        async: true,
        //请求成功
        success: function (result) {
            if (result.data == true) {
                $("#heart").attr("class", "fa fa-heart mr-1");
            } else {
                $("#heart").attr("class", "simple-icon-heart mr-1")
            }
        },
        //请求失败，包含具体的错误信息
        error: function (e) {
            showNotification("top", "提示", "请求失败！", "danger")
        }
    });
    $("#isLike").bind("click", function () {
        $.ajax({
            //请求方式
            type: "post",
            //请求的媒体类型
            // contentType: "application/json;charset=UTF-8",
            //请求地址
            url: "/blog/likes/",
            //数据，json字符串
            data: "articleId=" + id,
            dataType: "json",
            async: false,
            //请求成功
            success: function (result) {
                if (result.code == "0") {
                    if (result.data == true) {
                        $("#heart").attr("class", "fa fa-heart mr-1");
                        let likesText = +$("#likesCount").text() + 1
                        $("#likesCount").text(likesText)
                    } else {
                        $("#heart").attr("class", "simple-icon-heart mr-1")
                        let likesText = +$("#likesCount").text() - 1
                        $("#likesCount").text(likesText)
                    }
                } else {
                    showNotification("top", "提示", "请先登录！", "warning")
                }
            },
            //请求失败，包含具体的错误信息
            error: function (e) {
                showNotification("top", "提示", "请求失败！", "danger")
            }
        });
    });

    $("#commentBtn").bind("click", function () {
        let content = $("#commentContent").val();

        if (!content.trim().length < 1) {
            $.ajax({
                //请求方式
                type: "post",
                //请求的媒体类型
                contentType: "application/json;charset=UTF-8",
                // contentType: 'application/x-www-form-urlencoded;charset=UTF-8',
                //请求地址
                url: "/blog/comment/",
                data: JSON.stringify({commentContent: content, articleId: id}),
                //数据，json字符串
                dataType: "json",
                async: true,
                //请求成功
                success: function (result) {
                    if (result.code == 0) {
                        showNotification("top", "提示", "评论成功", "info")
                        $("#commentContent").val("");
                        let count = $("#commentCount").text();
                        $("#commentCount").text(parseInt(count) + 1);
                        queryComment();
                    } else {
                        showNotification("top", "提示", "请先登录！", "warning")
                        $("#commentContent").val("")
                    }
                },
                //请求失败，包含具体的错误信息
                error: function (e) {
                    showNotification("top", "提示", "请求失败！", "danger")
                }
            });
        }
    });

    $("#second-tab").bind("click", function () {
        if (!clickOne){
            queryComment();
        }

    });

    function queryComment() {
        $.ajax({
            //请求方式
            type: "get",
            //请求的媒体类型
            contentType: "application/json;charset=UTF-8",
            // contentType: 'application/x-www-form-urlencoded;charset=UTF-8',
            //请求地址
            url: "/blog/comment/"+id,
            //数据，json字符串
            dataType: "json",
            async: true,
            //请求成功
            success: function (result) {
                let htmlComment = "";
                result.data.forEach(function (listKey) {
                    console.log(listKey);
                    htmlComment += "<div class=\"d-flex flex-row mb-3 border-bottom justify-content-between\">\n" +
                        "                        <a href=\"#\">\n" +
                        "                        <img src="+listKey.account_profile_photo+" alt=\"Mimi Carreira\"\n" +
                        "                class=\"img-thumbnail border-0 rounded-circle list-thumbnail align-self-center xsmall\" />\n" +
                        "                        </a>\n" +
                        "                        <div class=\"pl-3 flex-grow-1\">\n" +
                        "                        <p class=\"font-weight-medium mb-0\">"+(listKey.account_nickname == null ? listKey.account_email : listKey.account_nickname)+"</p>\n" +
                        "                    <p class=\"text-muted mb-0 text-small\">"+new Date(listKey.comment_date).format("yyyy-MM-dd hh:mm:ss")+"</p>\n" +
                        "                    <p class=\"mt-3\">\n" +
                        "                        "+listKey.comment_content+"\n" +
                        "                    </p>\n" +
                        "                    </div>\n" +
                        "                    </div>"
                });
                $("#comment").html(htmlComment)
            },
            //请求失败，包含具体的错误信息
            error: function (e) {
                showNotification("top", "提示", "请求失败！", "danger")
            }
        });
    }

});